<!DOCTYPE html>
<!-- 
  Copyright 2011 Google Inc. All Rights Reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<html>
  <head>
    <title>Google I/O</title>

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1.1', {packages: ['corechart, geochart']});
    </script>
    <script type="text/javascript" src="../../js/io_xkcd_dilbert.js"></script>
    <script type="text/javascript">
      function avg3(x) {
        return 3 * google.visualization.data.avg(x);
      }

      function drawVisualization() {

        geodataFullTable = new google.visualization.DataTable();
        geodataFullTable.addColumn('date', 'Time');
        geodataFullTable.addColumn('string', 'Country');
        geodataFullTable.addColumn('number', 'xkcd Value');
        geodataFullTable.addColumn('number', 'Dilbert Value');
        geodataFullTable.addColumn('number', 'xkcd vs. dilbert');
        geodataFullTable.addRows(xkcdArray);

        geochartIsDrawing = false;
        geochart = new google.visualization.GeoChart(document.getElementById('geochart'));
        google.visualization.events.addListener(geochart, 'ready', function() {
            geochartIsDrawing = false;
        });
        timeselectData = new google.visualization.data.group(
            geodataFullTable,
            [0],
            [{column: 2, aggregation: google.visualization.data.avg, type: 'number'},
             {column: 3, aggregation: avg3, type: 'number'}]
            );
        timeselctDataView = new google.visualization.DataView(timeselectData);
        timeselctDataView.setRows(timeselctDataView.getFilteredRows(
            [{column: 0, minValue: new Date(2006, 0, 1), maxValue: new Date(2011, 0, 1)}]));

        timeselect = new google.visualization.RangeSelector(document.getElementById('timeselect'));
        google.visualization.events.addListener(timeselect, 'rangechange', updateSelection );
        timeselect.draw(timeselctDataView, {
          chartArea: {width: '90%', top: '10%', height: '70%'},
          vAxis: {textPosition: 'none', minValue: 0},
          hAxis: {type: 'value'},
          //colors: ['green', 'blue'],
          legend: 'none'
        });

        updateSelection({start: new Date(1960, 0, 1), end: new Date(2010, 0, 1)});
      }

      function updateSelection(e) {
        if (geochartIsDrawing) {
          return;
        }
        geodataView = new google.visualization.DataView(geodataFullTable);
        geodataView.setRows(geodataView.getFilteredRows(
            [{column: 0, minValue: e.start, maxValue: e.end}]));
        geodata = new google.visualization.data.group(
            geodataView,
            [1],
            [{column: 4, aggregation: google.visualization.data.avg, type: 'number'}]
            );
        geodata.addRows([['fake_max', 0.6], ['fake_min', -0.6]]);
        geochartIsDrawing = true;
        geochart.draw(geodata, {
          region: 'US',
          resolution: 'provinces',
          colors: ['red', '#fff', 'blue'],
          width: 600,
          height: 400,
        });
      }

      google.setOnLoadCallback(drawVisualization);
    </script>

  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="geochart" style="width: 600px; height: 400px;"></div>
    <div id="timeselect" style="width: 600px; height: 80px;"></div>
    <img src='../../img/new_badge.png'
         style='position: absolute; top: 20px; left: 500px; width: 100px'>
    <img src='../../img/soon_badge.png'
         style='position: absolute; top: 340px; left: 500px; width: 100px'>
  </body>
</html>
